package com.example.mntz.service.Impl;

import com.example.mntz.QuartzJob.AccJob;
import com.example.mntz.dao.AccountMapper;
import com.example.mntz.model.Account;
import com.example.mntz.model.Classs;
import com.example.mntz.service.AccService;
import com.example.mntz.util.QuartUtil;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class AccServiceImpl implements AccService {
    @Autowired
    AccountMapper mapper;
    @Autowired
    QuartUtil quartUtil;
    @Override
    public List<Classs> selectbq(Integer uid) {
        return mapper.selectbqs(uid);
    }

    @Override
    public Classs selectbq(Integer getuId, Integer classId) {
        return mapper.selectbq(getuId,classId);
    }

    @Override
    @Transactional
    public void insertClass(Classs claess) {

        try {
        if(claess.getClassId() !=null){
            //修改周期
            if(claess.getClassZq()!=null&& !"".equals(claess.getClassZq())){
                quartUtil.updateJob(claess.getClassId().toString(),"acc",claess.getClassZq());
            }else {
                quartUtil.deleteJob(claess.getClassId().toString(),"acc");
            }
            mapper.updateClass(claess);
        }else {
            mapper.insertClass(claess);
            //添加周期
            if(claess.getClassZq()!=null&& !"".equals(claess.getClassZq())) {
                quartUtil.addJob(AccJob.class, claess.getClassId().toString(), "acc", claess.getClassZq());
            }
        }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public List<Account> selectaccounts(String date,Integer uId) {
        return mapper.selectaccounts(date,uId);
    }

    @Override
    public Account selectacc(Integer accountId) {
        return mapper.selectByPrimaryKey(accountId);
    }

    @Override
    public void insertAccount(Account account) {
        if(account.getAccountId()!=null&&!"".equals(account.getAccountId())){
            mapper.updateByPrimaryKey(account);
        }else {
            mapper.insertAcc(account);
        }
    }

    @Override
    public List<Map> selecthPieClass(String time,String  redio,Integer uId) {
        return mapper.selecthPieClass(time,redio,uId);
    }
}
